#coding=utf-8
import time,os

from stutils import float2dec
from webscrapper.rtprice import get_rtprice
from stsettings import DIRNAME

LIVEPRICEDIR = DIRNAME + '/live/liveprice'
code = '600104'
ts = 1644551540
fn = '%s/%s.csv'%(LIVEPRICEDIR , code)

if os.path.exists(fn):
    f = open(fn, 'a')
else:
    f = open(fn, 'w')
    head = '时间,价格,成交量,换手率'


    f.write(head)
    f.write('\n')
    f.flush()

pre_v = None
while True:
    data = get_rtprice(code)    
    
    t = data[1]
    p = data[2]
    v = data[4]
    
    if (not pre_v) or pre_v != v:
    
        hsl = float(v) * 100 /ts
        hsl = str( float2dec(hsl, 4) )
        
        s = ','.join([t,p,v,hsl])
        f.write(s)
        f.write('\n')
        f.flush()
        
        pre_v = v
        
        time.sleep(0.5)
        
    else:
        print 'data have not changed right now.'
